﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using BlindData.Names;
using System.Diagnostics.Contracts;

namespace BlindData
{
	public class NameGenerator : INameGenerator
	{
		private static readonly Probability _middleNameProbability = new Probability(0.9);

		public IGenerator Generator { get; private set; }

		public NameGenerator(IGenerator generator)
		{
			Contract.Requires(generator != null);

			this.Generator = generator;
		}

		public Name NextName()
		{
			return NextName(_middleNameProbability);
		}

		public Name NextName(Probability middleNameProbability)
		{
			var firstNameFile = this.Generator.Next<bool>().Value() ? Resources.MaleFirstNameFile : Resources.FemaleFirstNameFile;
			var first = GetName(firstNameFile);
			var middle = this.Generator.Next<bool>().WithTrueProbability(middleNameProbability) ? GetName(firstNameFile) : null;
			var last = GetName(Resources.LastNameFile);

			return new Name(first, middle, last);
		}

		public string GetName(string nameFile)
		{
			var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(nameFile));

			//TODO: Pick a random seek value them move forward and back to find the begining and end of the value

			return null;
		}
	}
}
